package com.zjj.io.buffer;

import java.io.*;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class DirCopyTest {

    public static void main(String[] args) {
        copyDir(new File("D:\\a") , new File("D:\\temp"));
    }
    public static void copyDir(File srcDir, File destDir) {
        if(srcDir!=null && srcDir.isDirectory()){
            if(destDir!=null && !destDir.exists()){
                destDir.mkdirs();
            }
            File[] files = srcDir.listFiles();
            if(files!=null && files.length>0){
                for (File file : files) {
                    if(file.isDirectory()){
                        copyDir(new File(srcDir , file.getName()), new File(destDir , file.getName()));
                    }else if(file.isFile()){
                        copyFile(file , new File(destDir , file.getName()));
                    }
                }
            }
        }
        System.out.println("文件夹拷贝完成");
    }

    public static void copyFile(File srcFile, File destFile) {
        try (
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile))
        ){
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
